﻿using Frameworks;
using System;
using System.Collections.Generic;

namespace DotNetClient
{
    public class BroadcastFactory : Singleton<BroadcastFactory>
    {
        private BroadcastFactory() { }

        public override void OnSingletonInit()
        {
            base.OnSingletonInit();
            _callbacks = new Dictionary<string, Action<Message>>();
        }

        public override void Dispose()
        {
            base.Dispose();
            _callbacks.Clear();
        }

        public Dictionary<string, Action<Message>> Callbacks => _callbacks;
        private Dictionary<string, Action<Message>> _callbacks;

        public void RegistEvents()
        {
            // lobby
            _callbacks.Add(Routes.S2C_LOBBY_ON_CHAT, GameHandler.onPushUserChat);
            _callbacks.Add(Routes.S2C_LOBBY_ON_ENTER, GameHandler.onPushUserEnter);
            _callbacks.Add(Routes.S2C_LOBBY_ON_LEAVE, GameHandler.onPushUserLeave);
            // room
            _callbacks.Add(Routes.S2C_ROOM_ON_PLAYER_INFO, GameHandler.onPushPlayerInfo);
            _callbacks.Add(Routes.S2C_ROOM_ON_PLAYER_LEAVE, GameHandler.onPushPlayerLeave);
            _callbacks.Add(Routes.S2C_ROOM_ON_MATCH, GameHandler.onPushRoomMatch);
            // doudizhu
            _callbacks.Add(Routes.S2C_DOUDIZHU_ON_GAME_INIT, GameHandler.onPushDoudizhuGameInit);
            _callbacks.Add(Routes.S2C_DOUDIZHU_ON_GAME_START, GameHandler.onPushDoudizhuGameStart);
        }

        public void InvokeHandle(string route, Message data)
        {
            Action<Message> handler;
            _callbacks.TryGetValue(route, out handler);
            if (handler != null)
            {
                handler(data);
            }
            else
            {
                UnityEngine.Debug.LogWarning("cannot find handler for " + route);
            }
        }
    }
}
